home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5424 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  72 lines

  1. Newsgroups: comp.lang.c++
  2. Path: in2.uu.net!allegra!alice!bs
  3. From: bs@research.att.com (Bjarne Stroustrup <9758-26353> 0112760)
  4. Subject: Re: ? about declaration
  5. Message-ID: <DM9F2L.4Ht@research.att.com>
  6. Organization: Info. Sci. Div., AT&T Bell Laboratories, Murray Hill, NJ
  7. References: <4f2d0r$paq@noc2.drexel.edu>
  8. Date: Sun, 4 Feb 1996 16:27:57 GMT
  9.  
  10. st918h5w@dunx1.ocs.drexel.edu (Jonathan Juniman)
  11.  
  12.  > I am confused about something; I have been trying to declare new variables 
  13.  > inside of an if statement:
  14.  > 
  15.  > aClass::SomeFunction
  16.  >     {
  17.  >     aClass i();
  18.  > 
  19.  >     if(SomeCondition == TRUE)
  20.  >         {
  21.  >         aClass j();
  22.  >         }
  23.  >     }
  24.  > 
  25.  > Some compilers will let me do this, others will not.  I was under the
  26.  > impression that C++ always lets you do this, so that you don't have
  27.  > to allocate stack space for j unless SomeCondition is true.  Can
  28.  > someone straighten me out about what the standard is, or if there is
  29.  > none at all and this sort of thing is compiler dependent?  Please reply by
  30.  > mail.
  31.  
  32. There are (at least) three separate issues mixed together here:
  33.  
  34. Can you declared variables in a block within an if statement?
  35.  
  36.     Yes, and this was always the case from the very first C compiler.
  37.  
  38. Will memory for such variables be allocated only if their branch
  39. of the if-statement is entered?
  40.  
  41.     This is implementation dependent, the C and C++ standards say
  42.     nothing about that. Allocation of the memmory needed to hold a
  43.     local variable could be done only if its block was entered.
  44.     However, it is more common - and usually much more efficient -
  45.     to allocate local memory for all local variables (whether used
  46.     or unused in a particular call) when the function is called.
  47.  
  48.     Note that a local variable is initialized only if and when a
  49.     thread of execution reaches its definition.
  50.  
  51. What is the effect of putting empty parentheses after a name being
  52. declared?
  53.  
  54.     You declare a function. Thus, the example code above doesn't
  55.     declare two variables, it declares two functions. Consider:
  56.  
  57.     void X::f()
  58.     {
  59.         X i;     // declare a variable, default initialization
  60.         X j = 0; // declare a variable, initialize to 0
  61.         X k(0);  // declare a variable, initialize to 0
  62.         X l();   // declare a function taking no arguments
  63.     }
  64.  
  65.     Thus the () would redundant if you want to declare a variable,
  66.     and leads to a problem.
  67.  
  68.     The reason that ``X l();'' is a function declaration is that's
  69.     what it is (and always was) in C.
  70.  
  71.     - Bjarne
  72.